﻿using wscf.lightblue.Build;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Diagnostics;
using System.IO;

namespace wscf.lightblue.Test
{
	[TestClass()]
	public class SequenceTest : ActivityBaseTest
	{
		[TestMethod()]
		public void ExecuteTest()
		{
			string folder = "SequenceTest.ExecuteTest";
			Sequence s = CleanFolder(folder);
			s.Execute();
			Assert.AreEqual(0, CountFiles(folder));
			Assert.IsFalse(s.IsError);
		}

		private Sequence CleanFolder(string folder)
		{
			Sequence prep = new Sequence
			{
				Name = "Prepare testdata"
			};

			prep.Activities.Add(new InvokeProcess
			{
				Name = "Copy files",
				FileName = "xcopy",
				Arguments = string.Format("*.* {0} /d /y /q", folder),
				UseShellExecute = true
			});

			prep.Activities.Add(new InvokeProcess
			{
				Name = "Set files readonly",
				FileName = "attrib",
				Arguments = string.Format("{0}\\*.* +r", folder)
			});

			CleanFolder cf = new CleanFolder
			{
				Name = "Clean folder",
				Folder = folder
			};

			Sequence s = new Sequence
			{
				Name = "Sequence test"
			};

			s.Activities.Add(cf);
			s.Activities.Add(prep);
			s.Activities.Add(cf);

			return s;
		}

		private int CountFiles(string folder)
		{
			int count = 0;
			foreach (string file in Directory.EnumerateFiles(folder))
			{
				count++;
			}
			return count;
		}

		[TestMethod()]
		public void ExecuteFailTest()
		{
			string folder = "";
			Sequence s = CleanFolder(folder);
			s.Execute();
			Assert.IsTrue(s.IsError);
			
			s.ContinueOnError = true;
			s.Execute();
			Assert.IsTrue(s.IsError);
		}
	}
}
